﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ImeLib;
using MsIme = ImeLib.WinApi.MsIme;

namespace ImeLibDemo
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			cboImeType.Items.AddRange(MsImeFacade.ImeClassNames);
			cboImeType.SelectedIndex = (int) ImeClass.Taiwan;

			lblPhoneticImeInstalled.Text = ImmHelper.IsPhoneticImeInstalled().ToString();
			lblNewPhoneticImeInstalled.Text = ImmHelper.IsNewPhoneticImeInstalled().ToString();
		}

		private void UpdateUI(string[] result)
		{
			txtResult.Clear();
			txtResult.AppendText(cboImeType.Text + ":");
			txtResult.AppendText(Environment.NewLine);

			foreach (string s in result)
			{
				txtResult.AppendText(s);
				txtResult.AppendText(Environment.NewLine);
			}
		}

		private void btnGetJMorphResult_Click(object sender, EventArgs e)
		{
			ImeClass imeClass = (ImeClass)cboImeType.SelectedIndex;
			using (MsImeFacade ime = new MsImeFacade(imeClass)) 
			{
				// Show conversion mode capabilities.
				txtConvModeCaps.Text = ime.ConversionModeCaps;

				MsIme.Req req;
				MsIme.Cmodes cmodes;

				switch (imeClass) 
				{
					case ImeClass.China:
						req = MsIme.Req.Rev;
						cmodes = MsIme.Cmodes.PinYin | MsIme.Cmodes.NoInvisibleChar;						
						break;
					case ImeClass.Japan:
						req = MsIme.Req.Rev;
						cmodes = MsIme.Cmodes.HiraganaOut;
						break;
					case ImeClass.Taiwan:
						req = MsIme.Req.Rev;
						cmodes = MsIme.Cmodes.BoPoMoFo;
						break;
					case ImeClass.TaiwanBbo:
						req = MsIme.Req.Rev;
						cmodes = MsIme.Cmodes.BoPoMoFo;
						return;
					default:
						return;
				}

				string result = ime.GetJMorphResultString(txtInput.Text, req, cmodes);
				UpdateUI(new string[] { result });
			}			
		}

		private void btnGetBopomofo_Click(object sender, EventArgs e)
		{
			cboImeType.SelectedIndex = (int)ImeClass.Taiwan;

			using (MsImeFacade ime = new MsImeFacade(ImeClass.Taiwan))
			{
				string[] result = ime.GetBopomofo(txtInput.Text);
				UpdateUI(result);
			}			

		}

		private void btnGetPinYin_Click(object sender, EventArgs e)
		{
			cboImeType.SelectedIndex = (int)ImeClass.China;

			using (MsImeFacade ime = new MsImeFacade(ImeClass.China))
			{
				string result = ime.GetPinYin(txtInput.Text);
				UpdateUI(new string[] {result});
			}			
		}
	}
}
